package com.itboye.mengwu.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by itboye on 2017/4/17.
 */

public class NetState  extends BroadcastReceiver {

    public static boolean IS_ENABLE = true;
    private NetworkInfo gprs;
    private NetworkInfo wifi;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        // 监听网络状态
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        startActivity(context);
    }

    /**
     * 跳转
     *
     * @param context
     */
    private void startActivity(Context context) {
        if (!gprs.isConnected() && !wifi.isConnected() && IS_ENABLE) {
            System.out.println("start");
            IS_ENABLE = false;

//            Intent intent = new Intent().setClass(context,
//                    MainActivity.class);
//            ((Activity) context).startActivityForResult(intent, 1);


        }
    }
}
